class Solution {
public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        if (gas.size()==0) return -1;
        int n=gas.size();
        vector<int> diff(n, 0);
        for (int i=0;i<n;i++) diff[i]=gas[i]-cost[i];
        int sum=0;
        for (int i=0;i<n;i++) sum+=diff[i];
        if (sum<0) return -1;
        sum=0;
        int ans=0;
        for (int i=0;i<n;i++) {
            sum+=diff[i];
            if (sum<0) {
                sum=0;
                ans=i+1;
            }
        }
        return ans;
    }
};
